package com.moco.launcher.view;

import java.lang.reflect.Field;

import com.moco.launcher.listener.OnNeedleCallback;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewPropertyAnimator;
import android.widget.ImageView;

public class NeedleView extends ImageView {
	public static final float MAX_RANGE = 33.299999F;
	public static final float MIN_RANGE = 11.0F;
	private int[] MA;
	private double MB;
	private OnNeedleCallback MC;
	private float MD;
	private boolean ME = false;
	private ValueAnimator MF;
	private ValueAnimator MG;
	private ImageView Mw;
	private int Mx;
	private int My;
	private long Mz;
	public float mCurrentRange;

	public NeedleView(Context paramContext, AttributeSet paramAttributeSet) {
		super(paramContext, paramAttributeSet);
		init();
	}

	private double a(double paramDouble1, double paramDouble2) {
		double d1 = paramDouble1 - this.Mx;
		double d2 = -paramDouble2 + this.My;
		switch (b(d1, d2)) {
		default:
			return 0.0D;
		case 1:
			return 180.0D * Math.asin(d2 / Math.hypot(d1, d2)) / 3.141592653589793D;
		case 2:
		case 3:
			return 180.0D - 180.0D * Math.asin(d2 / Math.hypot(d1, d2)) / 3.141592653589793D;
		}
	}

	private void a(float paramFloat, Runnable paramRunnable) {
		if ((paramFloat < 11.0F) || (paramFloat > 33.299999F)) {
			paramFloat = 11.0F;
		}
		animate().rotation(paramFloat).scaleY(0.98F).setDuration(250L)
				.setListener(new AnimatorListener() {

					@Override
					public void onAnimationStart(Animator animation) {

					}

					@Override
					public void onAnimationEnd(Animator animation) {

					}

					@Override
					public void onAnimationCancel(Animator animation) {

					}

					@Override
					public void onAnimationRepeat(Animator animation) {

					}

				}).start();
		this.Mw.animate().rotation(paramFloat).scaleY(0.98F).setDuration(250L)
				.start();
	}

	private void a(ViewGroup paramViewGroup) {
		try {
			Field localField = Class.forName("android.view.ViewGroup")
					.getDeclaredField("mFirstTouchTarget");
			localField.setAccessible(true);
			localField.set(paramViewGroup, null);
		} catch (Exception localException) {
			localException.printStackTrace();
		}
	}

	private void a(ViewGroup paramViewGroup, View paramView) {
		a(paramViewGroup);
		long l = SystemClock.uptimeMillis();
		MotionEvent localMotionEvent = MotionEvent.obtain(l, l, 3, 0.0F, 0.0F,
				0);
		localMotionEvent.setSource(4098);
		paramView.dispatchTouchEvent(localMotionEvent);
		localMotionEvent.recycle();
	}

	private static int b(double paramDouble1, double paramDouble2) {
		if (paramDouble1 >= 0.0D) {
			if (paramDouble2 >= 0.0D) {
				return 1;
			}
			return 4;
		}
		if (paramDouble2 >= 0.0D) {
			return 2;
		}
		return 3;
	}

	private void b(Runnable paramRunnable) {
		animate().scaleY(1.0F).setDuration(250L)
				.setListener(new AnimatorListener() {

					@Override
					public void onAnimationStart(Animator animation) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onAnimationEnd(Animator animation) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onAnimationCancel(Animator animation) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onAnimationRepeat(Animator animation) {
						// TODO Auto-generated method stub

					}

				}).start();

		this.Mw.animate().scaleY(1.0F).rotation(getRotation())
				.setDuration(250L).start();
	}

	private boolean b(float paramFloat1, float paramFloat2, float paramFloat3,
			float paramFloat4) {
		return (paramFloat4 >= 0.8D * paramFloat2)
				&& (paramFloat4 <= paramFloat2);
	}

	private void dY() {
		dZ();
		if (this.MF != null) {
			this.MF.cancel();
			this.MF = null;
			this.MG.cancel();
			this.MG = null;
		}
		if (this.MC != null) {
			if (this.MD < 11.0F) {
				this.mCurrentRange = 11.0F;
				ea();
			}
		}

	}

	private void dZ() {
		setEnabled(false);
		((ViewGroup) getParent()).setEnabled(false);
		// postDelayed(new d(this), 300L);
	}

	private void ea() {
		ViewPropertyAnimator localViewPropertyAnimator = animate()
				.rotation(0.0F).scaleY(1.0F).setDuration(250L);
		// localViewPropertyAnimator.setListener(new e(this));
		localViewPropertyAnimator.start();
		this.Mw.animate().rotation(0.0F).scaleY(1.0F).setDuration(250L).start();
	}

	private void init() {
		Resources localResources = getResources();
		int i = 100;
		int j = 100;
		this.Mx = i;
		this.My = j;
		setPivotX(this.Mx);
		setPivotY(this.My);
	}

	private boolean k(MotionEvent paramMotionEvent) {
		float f1 = getRotation();
		setRotation(0.0F);
		if (this.MA == null) {
			this.MA = new int[2];
		}
		getLocationInWindow(this.MA);
		float f2 = paramMotionEvent.getX();
		float f3 = paramMotionEvent.getY();
		boolean bool = b(getWidth(), getHeight(), f2, f3);
		setRotation(f1);
		return bool;
	}

	private void l(float paramFloat) {
		if (paramFloat > 33.299999F) {
			paramFloat = 33.299999F;
		}
		for (;;) {
			this.MD = paramFloat;
			setRotation(paramFloat);
			if ((this.MF != null) && (!this.MF.isRunning())) {
				this.Mw.setRotation(paramFloat - 4.0F);
			}
			if (this.MC != null) {
				this.MC.onNeedleSeekTo(m(paramFloat));
				return;
			}
		}
	}

	private float m(float paramFloat) {
		if (paramFloat < 11.0F) {
			return 0.0F;
		}
		return 100.0F * ((paramFloat - 11.0F) / 22.299999F);
	}

	private int q(long paramLong) {
		if (this.Mz < paramLong) {
			return 0;
		}
		return (int) (this.Mz - paramLong);
	}

	private float r(long paramLong) {
		float f = 11.0F + 22.299999F * (float) paramLong / (float) this.Mz;
		if (Float.isNaN(f)) {
			f = 11.0F;
		}
		if (f > 33.299999F) {
			f = 33.299999F;
		}
		if (f < 11.0F) {
			f = 11.0F;
		}
		return f;
	}

	public void moveToPosition(long paramLong1, long paramLong2) {
		if ((paramLong1 < paramLong2) || (this.ME) || (paramLong2 < 0L)) {
		}
		float f;

		this.Mz = paramLong1;
		f = r(paramLong2);
		if (Math.abs(f - getRotation()) <= 2.0F)
			return;

		animate().rotation(f).setDuration(250L).start();
		this.Mw.animate().rotation(f).setDuration(250L).start();
	}

	public boolean onTouchEvent(MotionEvent paramMotionEvent) {
		int i = paramMotionEvent.getActionMasked();
		if ((i == 0) && ((!isEnabled()) || (!k(paramMotionEvent)))) {
			return false;
		}
		if (paramMotionEvent.getPointerCount() > 1) {
			a((ViewGroup) getParent(), this);
			return true;
		}
		int j = (int) (paramMotionEvent.getRawX() - this.MA[0]);
		int k = (int) (paramMotionEvent.getRawY() - this.MA[1]);
		switch (i) {
		}
		/*
		 * for (;;) { return true; pause(); up(); if (this.MC != null) {
		 * this.MC.onNeedleIsUp(); } this.MB = a(j, k); continue; double d =
		 * a(j, k); l((float)(this.MB - d) + getRotation()); this.MB = d;
		 * continue; dY(); }
		 */
		return false;
	}

	public void pause() {
		if (!this.ME) {
			animate().cancel();
			if (this.Mw != null) {
				this.Mw.animate().cancel();
			}
		}
	}

	public void play(long paramLong1, long paramLong2) {
		this.ME = false;
		if ((paramLong1 > 0L) && (paramLong1 > paramLong2)) {
			this.Mz = paramLong1;
			float f = r(paramLong2);
			if (Math.abs(f - getRotation()) >= 2.0F) {
				setRotation(f);
				this.Mw.setRotation(f);
			}
			int i = q(paramLong2);
			animate().rotation(33.299999F).setDuration(i).setListener(null)
					.start();
			this.Mw.animate().rotation(33.299999F).setDuration(i)
					.setListener(null).start();
		}
	}

	public void play(Runnable paramRunnable) {
		if (paramRunnable != null) {
			float f = getRotation();
			if ((f < 11.0F) || (f > 33.299999F)) {
				return;
			}
			if (getScaleY() == 1.0F) {
				paramRunnable.run();
			}
			b(paramRunnable);
		}
	}

	public void release() {
		getBackground().setCallback(null);
		this.Mw.getBackground().setCallback(null);
		animate().cancel();
		this.Mw.animate().cancel();
		this.MC = null;
	}

	public void reset() {
		float f = 11.0F;
		animate().rotation(f).setListener(null).setDuration(250L).start();
		ViewPropertyAnimator localViewPropertyAnimator = this.Mw.animate();
		if (getScaleY() == 1.0F) {
			f = 7.0F;
		}

		localViewPropertyAnimator.rotation(f).setDuration(250L).start();
	}

	public void setDuration(long paramLong) {
		this.Mz = paramLong;
	}

	public void setNeedleCallback(OnNeedleCallback paramonNeedleCallback) {
		this.MC = paramonNeedleCallback;
	}

	public void setShadowView(ImageView paramImageView) {
		this.Mw = paramImageView;
		paramImageView.setPivotX(this.Mx);
		paramImageView.setPivotY(this.My);
	}

	public void stop() {
		this.mCurrentRange = 0.0F;
		ea();
	}

	public void up() {
		if (this.MF != null) {
			this.MF.cancel();
			this.MF = null;
			this.MG.cancel();
			this.MG = null;
		}
		this.MF = ValueAnimator.ofFloat(new float[] { 1.0F, 0.98F });
		this.MF.setDuration(125L);
		this.MG = ValueAnimator.ofFloat(new float[] { 0.0F, 4.0F });
		this.MG.setDuration(125L);
		this.MF.start();
		this.MG.start();
	}
}
